package com.game.majiang.vo;

import java.sql.SQLException;
import java.sql.Types;

import javax.sql.rowset.CachedRowSet;

import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;

public class FriendbeizhuVo {

	public static void updateFriendsbeizhu2(String unionbeizhu,int uid,int createid) {

		String beizhuString="";
		String cmdText1 = "select id,beizhu from friendbeizhu where createid = "+createid+" and friendsid="+uid+"";
		CachedRowSet rs = SqlHelper.getResultSet(cmdText1);
		try {
			if (rs.next()) 
			{
				beizhuString=rs.getString(2);
				String cmdText = "update friendbeizhu set beizhu=?  where id = "+rs.getInt(1)+"";
				Parameter p11 = new Parameter(Types.VARCHAR, unionbeizhu, ParameterType.Input);
				SqlHelperwrite.execute(cmdText,p11); 
			}else {
				String sql = "INSERT INTO friendbeizhu (friendsid,createid,createtime,beizhu) VALUES (?,?,now(),?)";
				Parameter p1 = new Parameter(Types.INTEGER,  uid, ParameterType.Input);
				Parameter p2 = new Parameter(Types.INTEGER,  createid, ParameterType.Input);
				Parameter p3 = new Parameter(Types.VARCHAR,  unionbeizhu, ParameterType.Input);
				SqlHelperwrite.execute(sql,p1,p2,p3);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(beizhuString==null||"".equals(beizhuString)) {
			beizhuString="0";
		}
		String nowbeizhu=unionbeizhu;
		if(unionbeizhu==null||"".equals(unionbeizhu)) {
			nowbeizhu="0";
		}
		int type=0;
		int value=Integer.parseInt(nowbeizhu)-Integer.parseInt(beizhuString);
		if (value>=0) {
			type=1;
		}else {
			type=2;
		}
		

		InsertFriendupdateLog(value, type, uid, createid,Integer.parseInt(beizhuString));


		
		
	}
	/**
	 * 
	 * @param value
	 * @param type 1 ps  2rc  3chou
	 * @param uid 玩家id
	 * @param createid 好友房创建者id
	 */
	public static void InsertFriendupdateLog(int value,int type,int uid,int createid,int oldbeizhu) {

		String sql = "INSERT INTO friendsupdatelog (ps,rc,chou,uid,type,createid,createtime,oldbeizhu) VALUES (?,?,?,?,?,?,now(),?)";
		int ps=0;
		int rc=0;
		int chou=0;
		if(type==1) {
			 ps=value;
		 }else if(type==2) {
			 rc=value;
		 }else if(type==3) {
			 chou=value;
		 }
		 Parameter p1 = new Parameter(Types.INTEGER, ps, ParameterType.Input);
		 Parameter p2 = new Parameter(Types.INTEGER, rc, ParameterType.Input);
		 Parameter p3 = new Parameter(Types.INTEGER, chou, ParameterType.Input);
		 Parameter p4 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		 Parameter p5 = new Parameter(Types.INTEGER, type, ParameterType.Input);
		 Parameter p6 = new Parameter(Types.INTEGER, createid, ParameterType.Input);
		 Parameter p7 = new Parameter(Types.INTEGER, oldbeizhu, ParameterType.Input);
		 SqlHelperwrite.execute(sql,p1,p2,p3,p4,p5,p6,p7);
		
	}
	

	
	public static String selectzhanji(int uid,int createid){
		
			String beizhuString="";
			String cmdText = "select beizhu from friendbeizhu where friendsid=? and createid=? ORDER BY createtime DESC";
			Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
			Parameter p2 = new Parameter(Types.INTEGER,createid, ParameterType.Input);
			CachedRowSet rs = SqlHelper.getResultSet(cmdText,p1,p2);
			try {
				if(rs.next()) 
				{
					beizhuString=rs.getString(1);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
		return beizhuString;
	}
	

}
